﻿/*
	Copyright (c) 2009, M.P. Bartnig & A. Gosman
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
package nl.sanity.validation 
{
	/**
	 * ...
	 * @author A. Gosman
	 */
	public class DateValidator 
	{
		public static var SEPARATOR 		: String		= "-";
		
		public static const FORMAT_DDMMYYYY	: String 		= "ddmmyyyy";
		public static const FORMAT_MMYYYY	: String 		= "mmyyyy";
		
		public static const DAYS_IN_MONTHS 	: Array			= [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
		
		
		public static function isValid ( date:String, format:String = FORMAT_DDMMYYYY ):Boolean 
		{
			switch ( format )
			{
				case FORMAT_DDMMYYYY:
					return validateDDMMYYYY( date );
					
				case FORMAT_MMYYYY:
					return validateMMYYYY( date );
					
			}
			
			return true;
		}
		
		public static function validateDDMMYYYY( date:String ):Boolean
		{
			var dateValues:Array 	= date.split( SEPARATOR );
			
			var day:Number			= Number( dateValues [ 0 ] );
			var month:Number		= Number( dateValues [ 1 ] );
			var year:Number			= Number( dateValues [ 2 ] );
			
			if ( isNaN( day ) || isNaN( month ) || isNaN( year ) )
			{
				return false;
			}
			
			if ( day < 1 || month < 1 || year < 0 )
			{
				return false;
			}
			
			if ( month > 12 || day > getDaysInMonth( month, year ) )
			{
				return false;
			}
			
			return true;
		}
		
		public static function validateMMYYYY( date:String ):Boolean
		{
			var dateValues:Array 	= date.split( SEPARATOR );
			
			var month:Number		= Number( dateValues [ 0 ] );
			var year:Number			= Number( dateValues [ 1 ] );
			
			if ( isNaN( month ) || isNaN( year ) )
			{
				return false;
			}
			
			if ( month < 1 || year < 0 || month > 12 )
			{
				return false;
			}
			
			return true;
		}
		
		/**
		 * 	Returns the number of days within the specified month.
		 * 
		 */
		public static function getDaysInMonth(  month:uint, year:uint ):uint
		{
			if ( month == 2 && isLeapYear( year ) )	return 29;
			else 									return DAYS_IN_MONTHS[ month - 1 ];
		}
		
		/**
		 * Checks whether a year is a leap year or not.
		 * 
		 * @param		year	The year to test.
		 * @return				True when the provided year is a leap year, false if not.
		 * @see 				http://en.wikipedia.org/wiki/Leap_year#Algorithm
		 */
		public static function isLeapYear( year:uint ):Boolean
		{
		   return ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 );
		}
	}
}